*&---------------------------------------------------------------------*
*& Report YBC_SWS_DEMO01
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT YBC_SWS_DEMO02.

DATA:
  LS_DP_HEAD TYPE /OPT/VIM_1HEAD,
  LT_DP_ITEM TYPE STANDARD TABLE OF /OPT/VIM_1ITEM.

DATA:
  LV_AMOUNT    TYPE P DECIMALS 2,
  LV_BUSID     TYPE YBC_SWS_REQUEST-BUSINESSID,
  LS_HEAD_DATA TYPE YBCS_SWS_DATA_H,
  LS_ITEM_DATA TYPE YBCS_SWS_DATA_I,
  LT_HEAD_DATA TYPE STANDARD TABLE OF YBCS_SWS_DATA_H,
  LT_ITEM_DATA TYPE STANDARD TABLE OF YBCS_SWS_DATA_I,
  LS_APPROVER  TYPE YBCS_SWS_APPROVER,
  LT_APPROVER  TYPE STANDARD TABLE OF YBCS_SWS_APPROVER,
  LS_ATTACH    TYPE YBCS_SWS_ATTACH01,
  LT_ATTACH    TYPE YBCT_SWS_ATTACH01,
  LS_MSG       TYPE BAPIRET2,
  LV_MESSAGE   TYPE STRING.

DATA:
  LV_DATA_X      TYPE XSTRING,
  LV_LEN         TYPE I,
  LV_INDEX       TYPE I,
  LV_MINETYPE    TYPE CHAR200,
  LV_CONTENTTYPE TYPE STRING,
  LT_DATA_OPT    TYPE STANDARD TABLE OF SDOKCNTBIN WITH HEADER LINE.

SELECT-OPTIONS:
  S_DP FOR LS_DP_HEAD-DOCID.

INITIALIZATION.

AT LINE-SELECTION.

START-OF-SELECTION.

  SELECT SINGLE *
    FROM /OPT/VIM_1HEAD
    INTO LS_DP_HEAD
   WHERE DOCID IN S_DP.


  SELECT *
    FROM /OPT/VIM_1ITEM
    INTO TABLE LT_DP_ITEM
   WHERE DOCID IN S_DP.

  SELECT *
    FROM TOA01
    INTO TABLE @DATA(LT_TOA01)
   WHERE OBJECT_ID IN @S_DP
     AND SAP_OBJECT = '/OPT/V1001'.

  LV_BUSID = LS_DP_HEAD-DOCID.

  CALL FUNCTION 'YBC_SWS_CANCEL_REQUEST'
    EXPORTING
*     IV_REQUESTID      =
      IV_APPID          = 'INV_NPO'
      IV_BUSINESSID     = LV_BUSID
      IV_NO_EXIST_CHECK = 'X'
*     IV_NO_STATUS_CHECK       =
*   IMPORTING
*     ES_MSG            =
    .

  "根据结构定义，自动赋值抬头行项目值
  CALL FUNCTION 'YBC_SWS_PREPARE_DATA'
    EXPORTING
      IV_HEADER    = LS_DP_HEAD
    IMPORTING
      ES_MSG       = LS_MSG
    TABLES
      IT_ITEM      = LT_DP_ITEM
      ET_HEAD_DATA = LT_HEAD_DATA
      ET_ITEM_DATA = LT_ITEM_DATA.

  "添加自定义字段，不在DP_head结构中
  CLEAR LS_HEAD_DATA.
  LS_HEAD_DATA-VALUE = '自定义字段值'.
  LS_HEAD_DATA-FIELD = 'ZZADD'.
  CONDENSE LS_HEAD_DATA-VALUE.
  APPEND LS_HEAD_DATA TO LT_HEAD_DATA.

  WRITE: / 'PREPARE_DATA' , LS_MSG-MESSAGE.

  IF LS_MSG-TYPE <> 'E'.

    LOOP AT LT_TOA01 INTO DATA(LS_TOA01).
      CLEAR LS_ATTACH.

      CALL FUNCTION 'SCMS_HTTP_GET'
        EXPORTING
*         MANDT                 = SY-MANDT
          CREP_ID               = LS_TOA01-ARCHIV_ID
          DOC_ID                = LS_TOA01-ARC_DOC_ID
        IMPORTING
          LENGTH                = LV_LEN
          MIMETYPE              = LV_MINETYPE
        TABLES
          DATA                  = LT_DATA_OPT
        EXCEPTIONS
          BAD_REQUEST           = 1
          UNAUTHORIZED          = 2
          NOT_FOUND             = 3
          CONFLICT              = 4
          INTERNAL_SERVER_ERROR = 5
          ERROR_HTTP            = 6
          ERROR_URL             = 7
          ERROR_SIGNATURE       = 8
          OTHERS                = 9.

      IF LT_DATA_OPT[] IS NOT INITIAL.

        CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
          EXPORTING
            INPUT_LENGTH = LV_LEN
          IMPORTING
            BUFFER       = LS_ATTACH-CONTENT
          TABLES
            BINARY_TAB   = LT_DATA_OPT
          EXCEPTIONS
            FAILED       = 1
            OTHERS       = 2.

      ENDIF.

      LS_ATTACH-FILENAME = LS_TOA01-ARC_DOC_ID.
      LS_ATTACH-FILESIZE = LV_LEN.
      LS_ATTACH-FILETYPE = 'pdf'.
      LS_ATTACH-MIMETYPE = LV_MINETYPE.
      APPEND LS_ATTACH TO LT_ATTACH.

    ENDLOOP.

    CALL FUNCTION 'YBC_SWS_SEND_FIRST'
      EXPORTING
        IV_APPID      = 'INV_NPO'
        IV_TEMPLID    = 'INV_NPO'
        IV_BUSINESSID = LV_BUSID
*       IV_UNAME      = SY-UNAME
      IMPORTING
        ES_MSG        = LS_MSG
      TABLES
        IT_HEAD_DATA  = LT_HEAD_DATA
        IT_ITEM_DATA  = LT_ITEM_DATA
*       IT_APPROVER   =
        IT_ATTACH     = LT_ATTACH.
  ENDIF.

  WRITE: / 'Send First', LS_MSG-MESSAGE.
